package demo.miner;

import java.util.ArrayList;
import java.util.List;

import jtech.math.Vec2f;

public class Path {

    private List<Vec2f> wayPoints;
    private boolean looped;

    private int current;

    public Path() {
        
    }
    
    public Path(List<Vec2f> wayPoints) {
        this.wayPoints = wayPoints;
    }
    
    public boolean isLooped() {
        return looped;
    }

    public void setLooped(boolean looped) {
        this.looped = looped;
    }

    public Vec2f getCurrentWaypoint() {
        return getWayPoints().get(current);
    }

    public int size() {
        return getWayPoints().size();
    }
    
    public void xxx(Vec2f pos, float r) {
    	
    	for (int i = current; i < size(); i++) {
    		Vec2f p = getWayPoints().get(i);
    		if (p.distanceSq(pos) < r*r) {
    			current = i;
    			break;
    		}
    	}
    	
    }
    
    // returns true if the end of the list has been reached
    public boolean isFinished() {
        return current >= size() - 1;
    }
    
    public void nextWaypoint() {
        if (current < size() - 1) {
            current++;
        }
        if (current == size() && looped) {
            current = 0;
        }
    }
    
    public void addWayPoint(Vec2f wayPoint) {
        getWayPoints().add(wayPoint);
    }
    
    public void addWayPoint(List<Vec2f> wayPoints) {
    	for (Vec2f wayPoint : wayPoints) {
    		getWayPoints().add(wayPoint);
    	}
    }
    
    public List<Vec2f> getWayPoints() {
        if (wayPoints == null) {
            wayPoints = new ArrayList<>();
        }
        return wayPoints;
    }

    public void setWayPoints(List<Vec2f> wayPoints) {
        this.wayPoints = wayPoints;
        current = 0;
    }
  
    public void clear() {
        current = 0;
        getWayPoints().clear();
    }

}
